
//
//  YDOAOilVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/6/8.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit

class YDOAOilVC: UIViewController {

    @IBOutlet weak var segmentContor: UISegmentedControl!
    
    
    @IBOutlet weak var myTableView: UITableView!//油卡充值
    
    @IBOutlet weak var catTableView: UITableView!
    
    
    
    lazy var statusArr : Array  = Array<Int>() //保存所有头部的状态
    lazy var statusArr1 : Array  = Array<Int>() //保存所有头部的状态
    lazy var headerViews : Array = Array<UIView>() //保存所有头部视图
    
    var tableViewType : Int = 0
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        myTableView.register(UINib(nibName: "YAODCatOilcell", bundle: nil), forCellReuseIdentifier: "YAODCatOilcell")
        
        catTableView.register(UINib(nibName: "YAODCatOilcell", bundle: nil), forCellReuseIdentifier: "YAODCatOilcell")
        for _ in 0..<300 {
            statusArr.append(0)
        }
        for _ in 0..<10 {
            statusArr1.append(0)
        }
        headerViews.removeAll()
        myTableView.reloadData()
        catTableView.reloadData()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //发起申请按钮点击
    @IBAction func sureBtnClick(_ sender: Any) {
        
        if tableViewType == 0 {
            YMLog.log(item: "油卡申请发起申请被点击")
            navigationController?.pushViewController(YDOAOilUseVC(), animated: true)

        }else{
            YMLog.log(item: "汽车加油发起申请被点击")
            navigationController?.pushViewController(YDOACatRechargeVC(), animated: true)
        }
        
        
    }

   
}


extension YDOAOilVC {

    fileprivate func setupUI(){
          segmentContor.backgroundColor = UIColor(190, 190, 190)
             segmentContor.addTarget(self, action: #selector(segMentChange), for: .valueChanged)
    }
    @objc fileprivate func segMentChange(){
        if segmentContor.selectedSegmentIndex == 0{
            myTableView.isHidden = false
            catTableView.isHidden = true
            tableViewType = 0
            myTableView.reloadData()
        }else{
            catTableView.isHidden = false
            myTableView.isHidden = true
            tableViewType = 1
            catTableView.reloadData()
        }
    }
    

}







extension  YDOAOilVC : UITableViewDelegate, UITableViewDataSource {
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if tableViewType == 0 {
            return statusArr.count
        }else{
            return statusArr1.count

        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if tableViewType == 0 {
            if statusArr[section] == 0 {
                return 0
            }
            return 1
        }else{
            if statusArr1[section] == 0 {
                return 0
            }
            return 1
        }
       
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard  let cell = tableView.dequeueReusableCell(withIdentifier: "YAODCatOilcell") as? YAODCatOilcell else {
            return YAODCatOilcell()
        }
        
        cell.index = indexPath.section
        
        cell.callback = {(id : Int , index : Int) in
        
            YMLog.log(item: "点击了id = \(id) 下标 \(index) 行")
            
        }
        
        return  cell
        
        
    }
    
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        //油卡充值部分的头部
        if tableViewType == 0{
        //删除所有视图
//        let reuserId = "herderView"
//        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: reuserId)
//        if headerView == nil {
            let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
            
            let oilnumLab = UILabel(frame: CGRect(x: 15, y: 20, width: ScreenWidth - 30, height: 21))
                oilnumLab.text = "油卡编号：465151521311515151561651"
                oilnumLab.textColor = UIColor.black
                oilnumLab.font = UIFont.systemFont(ofSize: 15)
        
            let payCountLab = UILabel(frame: CGRect(x: 15, y: oilnumLab.ym_bottomY+8, width:ScreenWidth - 30, height: 21))
                payCountLab.text = "充值金额: 10000"
                payCountLab.textColor = UIColor.black
                payCountLab.font = UIFont.systemFont(ofSize: 15)
        
            let timeLab = UILabel(frame: CGRect(x: 15, y: payCountLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
                timeLab.text = "充值时间:2017年4月14日"
                timeLab.textColor = UIColor.black
                timeLab.font = UIFont.systemFont(ofSize: 15)
        
        
            view1.tag = 500 + section
            let tap = UITapGestureRecognizer(target: self, action: #selector(click))
            view1.addGestureRecognizer(tap)
            let  lineView = UIView(frame: CGRect(x: 0, y: 117, width: ScreenWidth, height: 1))
            lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
            view1.addSubview(lineView)
            view1.backgroundColor = UIColor.white
        
            view1.addSubview(oilnumLab)
            view1.addSubview(payCountLab)
            view1.addSubview(timeLab)
            
            if headerViews.count < statusArr.count{
                headerViews.append(view1)
            }
  
            return view1
        
        }else{//汽车加油头部
            let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
            
            let oilnumLab = UILabel(frame: CGRect(x: 15, y: 20, width: ScreenWidth - 30, height: 21))
            oilnumLab.text = "油卡编号：465151521311515151561651"
            oilnumLab.textColor = UIColor.black
            oilnumLab.font = UIFont.systemFont(ofSize: 15)
            
            let payCountLab = UILabel(frame: CGRect(x: 15, y: oilnumLab.ym_bottomY+8, width:ScreenWidth - 30, height: 21))
            payCountLab.text = "消耗金额: 10000"
            payCountLab.textColor = UIColor.black
            payCountLab.font = UIFont.systemFont(ofSize: 15)
            
            let timeLab = UILabel(frame: CGRect(x: 15, y: payCountLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            timeLab.text = "加油时间:2017年4月14日"
            timeLab.textColor = UIColor.black
            timeLab.font = UIFont.systemFont(ofSize: 15)
            
            //申请人
            let personLab = UILabel(frame: CGRect(x: 15, y: timeLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            personLab.text = "申请人：曾诚"
            personLab.textColor = UIColor.black
            personLab.font = UIFont.systemFont(ofSize: 15)
            //油号
            let oilNumLab = UILabel(frame: CGRect(x: 15, y: personLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            oilNumLab.text = "油号：93#"
            oilNumLab.textColor = UIColor.black
            oilNumLab.font = UIFont.systemFont(ofSize: 15)
            
            //价格
            let priceLab = UILabel(frame: CGRect(x: 15, y: oilNumLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            priceLab.text = "价格：7.3￥/L"
            priceLab.textColor = UIColor.black
            priceLab.font = UIFont.systemFont(ofSize: 15)
            
            
            //卡内余额
            let remainNumLab = UILabel(frame: CGRect(x: 15, y: priceLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            remainNumLab.text = "卡内余额：12152.03"
            remainNumLab.textColor = UIColor.black
            remainNumLab.font = UIFont.systemFont(ofSize: 15)
            
            view1.tag = 5000 + section
            let tap = UITapGestureRecognizer(target: self, action: #selector(click))
            view1.addGestureRecognizer(tap)
            let  lineView = UIView(frame: CGRect(x: 0, y: 249, width: ScreenWidth, height: 1))
            lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
            view1.addSubview(lineView)
            view1.backgroundColor = UIColor.white
            
            view1.addSubview(oilnumLab)
            view1.addSubview(payCountLab)
            view1.addSubview(timeLab)
            view1.addSubview(personLab)
            view1.addSubview(oilNumLab)
            view1.addSubview(priceLab)
            view1.addSubview(remainNumLab)
            if headerViews.count < statusArr.count{
                headerViews.append(view1)
            }
            
            return view1
        }
    }
    
    
    //MARK: - 点击事件
    @objc private func click(tap : UIGestureRecognizer){
        
        if tableViewType == 0{
            let tag = (tap.view?.tag)! - 500
            if statusArr[tag] == 0 {
                statusArr[tag] = 1
            }else{
                statusArr[tag] = 0
            }
            headerViews.removeAll()
            myTableView.reloadData()
        }else{
            let tag = (tap.view?.tag)! - 5000
            if statusArr1[tag] == 0 {
                statusArr1[tag] = 1
            }else{
                statusArr1[tag] = 0
            }
            headerViews.removeAll()
            catTableView.reloadData()
        }
            
       
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if tableViewType == 0{
              return 118
        }else {
            return 250
        }
      
    }
    
}
